﻿using UnityEngine;
using System;
using System.Collections.Generic;
using DG.Tweening;

//背景音乐
public enum EBgmType
{
    BG1intro,
    BG1loop,
    BG2intro,
    BG2loop,
    BG3intro,
    BG3loop,
}
//音效音乐
public enum ESoundType
{
    None = 0,
    
}

public class AudioController
{
    public static AudioController Instance => _instance ?? (_instance = new AudioController());
    private static AudioController _instance;
    
    public event Action<bool> OnBgmMuteChanged;
    public bool BgmMute
    {
        get => m_bgmMute;
        set
        {
            if (m_bgmMute == value) return;
            m_bgmMute = m_bgmAudioPlayer.mute = value;
            PlayerPrefs.SetInt(KeyStore.Audio.BGM_MUTE, m_bgmMute ? 1 : 0);
            OnBgmMuteChanged?.Invoke(value);
        }
    }
    public float BgmVolume
    {
        get => m_bgmVolume;
        set
        {
            m_bgmVolume = m_bgmAudioPlayer.volume = value;
            PlayerPrefs.SetFloat(KeyStore.Audio.BGM_VOLUME, m_bgmVolume);
        }
    }

    public bool SoundMute
    {
        get => m_soundMute;
        set
        {
            if (m_soundMute == value) return;
            m_soundMute = m_soundAudioPlayer.mute = value;
            PlayerPrefs.SetInt(KeyStore.Audio.SOUND_MUTE, m_soundMute ? 1 : 0);
        }
    }
    public float SoundVolume
    {
        get => m_soundVolume;
        set
        {
            m_soundVolume = m_soundAudioPlayer.volume = value;
            PlayerPrefs.SetFloat(KeyStore.Audio.SOUND_VOLUME, m_soundVolume);
        }
    }

    private readonly AudioSource m_bgmAudioPlayer;
    private readonly AudioSource m_soundAudioPlayer;
    //缓存
    private readonly Dictionary<ESoundType, AudioClip> m_soundClipCacheMap = new Dictionary<ESoundType, AudioClip>();
    private readonly Dictionary<EBgmType, AudioClip> m_bgmClipCacheMap = new Dictionary<EBgmType, AudioClip>();
    //静音/音量
    private bool m_bgmMute;
    private float m_bgmVolume;
    private bool m_soundMute;
    private float m_soundVolume;

    private Sequence m_bgmFadeAnim;

    public AudioSource BG;
     

    public AudioController()
    {
        _instance = this;
        
        //读取存档
        m_bgmMute = PlayerPrefs.GetInt(KeyStore.Audio.BGM_MUTE, 0) == 1;
        m_bgmVolume = PlayerPrefs.GetFloat(KeyStore.Audio.BGM_VOLUME, 1f);
        m_soundMute = PlayerPrefs.GetInt(KeyStore.Audio.SOUND_MUTE, 0) == 1;
        m_soundVolume = PlayerPrefs.GetFloat(KeyStore.Audio.SOUND_VOLUME, 1f);
        
        //背景音乐
        m_bgmAudioPlayer = new GameObject("BgmPlayer").AddComponent<AudioSource>();
        m_bgmAudioPlayer.Stop();
        m_bgmAudioPlayer.loop = true;
        m_bgmAudioPlayer.mute = m_bgmMute;
        m_bgmAudioPlayer.volume = m_bgmVolume;
        //音效
        m_soundAudioPlayer = new GameObject("SoundPlayer").AddComponent<AudioSource>();
        m_soundAudioPlayer.Stop();
        m_soundAudioPlayer.mute = m_soundMute;
        m_soundAudioPlayer.volume = m_soundVolume;
        
        //专属音效播放器
        var temp = new GameObject("AudioPlayerTemplate");
        var audioSource = temp.AddComponent<AudioSource>();
        audioSource.Stop();
        audioSource.mute = m_soundMute;
        audioSource.volume = m_soundVolume;
    }

    /// <summary>
    /// 播放背景音乐
    /// </summary>
    public float PlayBgm(EBgmType type)
    {
        m_bgmFadeAnim?.Complete(true);
        var clip = GetAudioClip(type);
        if (m_bgmAudioPlayer.isPlaying)
        {
            //避免重复播放
            if (m_bgmAudioPlayer.clip == clip) return -1;
            /*//淡出淡入
            m_bgmFadeAnim = DOTween.Sequence();
            m_bgmFadeAnim.Append(m_bgmAudioPlayer.DOFade(0, 0.5f));
            m_bgmFadeAnim.AppendCallback(() =>
            {*/
                m_bgmAudioPlayer.clip = clip;
                m_bgmAudioPlayer.Play();
            /*});
            m_bgmFadeAnim.Append(m_bgmAudioPlayer.DOFade(m_bgmVolume, 0.5f));
            m_bgmFadeAnim.AppendCallback(() => m_bgmFadeAnim = null);*/
            return m_bgmAudioPlayer.clip.length;
        }
        else
        {
            m_bgmAudioPlayer.clip = clip;
            m_bgmAudioPlayer.Play();
            return m_bgmAudioPlayer.clip.length;
            /*//淡入
            m_bgmFadeAnim = DOTween.Sequence();
            m_bgmFadeAnim.Append(m_bgmAudioPlayer.DOFade(m_bgmVolume, 0.5f).From(0));*/
        }
    }


    /// <summary>
    /// 停止背景音乐
    /// </summary>
    public void StopBgm()
    {
        m_bgmFadeAnim?.Complete(true);
        m_bgmFadeAnim = DOTween.Sequence();
        m_bgmFadeAnim.Append(m_bgmAudioPlayer.DOFade(0, 0.5f));
        m_bgmFadeAnim.AppendCallback(() =>
        {
            m_bgmAudioPlayer.volume = m_bgmVolume;
            m_bgmAudioPlayer.Stop();
        });
    }
    /// <summary>
    /// 播放音效
    /// </summary>
    public void PlaySound(ESoundType type)
    {
        if (m_soundMute) return;
        var clip = GetAudioClip(type);
        m_soundAudioPlayer.PlayOneShot(clip);
    }

    public AudioClip GetAudioClip(EBgmType type)
    {
        if (!m_bgmClipCacheMap.TryGetValue(type, out var clip))
        {
            clip = Resources.Load<AudioClip>($"BGM/{type.ToString()}");
            Debug.Log(clip.length);
            m_bgmClipCacheMap.Add(type, clip);
        }
        Debug.Log(clip.length);
        return clip;
    }
    public AudioClip GetAudioClip(ESoundType type)
    {
        if (!m_soundClipCacheMap.TryGetValue(type, out var clip))
        {
            clip = Resources.Load<AudioClip>($"Sound/{type.ToString()}");
            m_soundClipCacheMap.Add(type, clip);
        }
        return clip;
    }
}

